<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta
            name="viewport"
            content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
    >
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>元素拖拽</title>
    <script src="../../utils/label-log.js"></script>
    <style>
        body {
            user-select: none;
            display: grid;
            place-content: center;
        }
        main {
            display: flex;
            height: 500px;
            width: 600px;
        }
        .left,.right {
            border: 1px solid black;
            flex: 1;
            display: flex;
            flex-direction: column;
            padding: 10px;
        }
        .list-item {
            margin-bottom: 10px;
            background: antiquewhite;
            padding: 5px 20px;
            width: 50px;
            text-align: center;
        }
        .dragging {
            background: coral;
        }
    </style>
</head>
<body>
<main>
    <section class="left"></section>
    <section class="right"></section>
</main>
<script>
    const left = document.querySelector(".left");
    const right = document.querySelector(".right");
    const sourceData = [];
    const selectData = [];
</script>
<script>
    left.addEventListener("drag", function(ev) {
        labelLog("left", 1, "drag", ev);
    });
    left.addEventListener("dragstart", /*** @param {DragEvent} ev*/function(ev) {
        labelLog("left", 1, "dragstart", ev);
        ev.target.classList.add("dragging");
        const dataIndex = ev.target.dataset.index;
        ev.dataTransfer.setData("dataIndex", dataIndex);
    });
    left.addEventListener("dragend", function(ev) {
        labelLog("left", 1, "dragend", ev);
        ev.target.classList.remove("dragging");
    });
</script>
<script>
    right.addEventListener("dragover", function(ev) {
        ev.preventDefault();
        labelLog("left", 2, "dragover", ev);
    });
    right.addEventListener("dragenter", function(ev) {
        labelLog("left", 2, "dragenter", ev);
    });
    right.addEventListener("drop", /***@param {DragEvent} ev*/function(ev) {
        ev.preventDefault();
        labelLog("left", 2, "drop", ev);
        const dropDataIndex = ev.dataTransfer.getData("dataIndex");
        const dropData = sourceData.splice(+dropDataIndex, 1);
        selectData.push(...dropData);
        renderUI();
    });
</script>
<script>
    function loadData() {
        sourceData.push("A","B","C","D","E")
    }
    function renderListItem(text, index) {
        const item = document.createElement("div");
        item.classList.add("list-item");
        item.textContent = text;
        item.draggable = true;
        item.dataset.index = index;
        return item;
    }

    /**
     * 
     * @param {HTMLElement} container
     * @param data
     */
    function renderList(container, data) {
        const frag = document.createDocumentFragment();
        for (const [index, item] of data.entries()) {
            frag.appendChild(renderListItem(item, index));
        }
        while (container.hasChildNodes()){
            container.removeChild(container.firstChild);
        }
        container.appendChild(frag);
    }
    function renderUI() {
        renderList(left, sourceData);
        renderList(right, selectData);
    }
</script>
<script>
    loadData();
    renderUI();
</script>
</body>
</html>